home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / dpmainc.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  8.2 KB  |  311 lines

  1. /* Copyright (C) 1996, Russell Lang.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19.  
  20. /*$Id: dpmainc.c,v 1.2 2000/09/19 19:00:09 lpd Exp $ */
  21. /* Ghostscript DLL loader for OS/2 */
  22. /* For WINDOWCOMPAT (console mode) application */
  23.  
  24. /* Russell Lang  1996-06-05 */
  25.  
  26. #define INCL_DOS
  27. #define INCL_WIN
  28. #include <os2.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include "gscdefs.h"
  32. #define GS_REVISION gs_revision
  33. #include "gsdll.h"
  34. #include "gsdllos2.h"
  35.  
  36. #define MAXSTR 256
  37. const char *szDllName = "GSDLL2.DLL";
  38. char start_string[] = "systemdict /start get exec\n";
  39. int debug = FALSE;
  40.  
  41. /* main structure with info about the GS DLL */
  42. typedef struct tagGSDLL {
  43.     BOOL valid;            /* true if loaded */
  44.     HMODULE hmodule;        /* handle to module */
  45.     /* pointers to DLL functions */
  46.     PFN_gsdll_revision revision;
  47.     PFN_gsdll_init init;
  48.     PFN_gsdll_exit exit;
  49.     PFN_gsdll_execute_begin execute_begin;
  50.     PFN_gsdll_execute_cont execute_cont;
  51.     PFN_gsdll_execute_end execute_end;
  52.     PFN_gsdll_get_bitmap get_bitmap;
  53.     PFN_gsdll_lock_device lock_device;
  54.     /* pointer to os2dll device */
  55.     char *device;
  56. } GSDLL;
  57. GSDLL gsdll;
  58.  
  59. void
  60. gs_addmess(char *str)
  61. {
  62.     fputs(str, stdout);
  63. }
  64.  
  65. /* free GS DLL */
  66. /* This should only be called when gsdll_execute has returned */
  67. /* TRUE means no error */
  68. BOOL
  69. gs_free_dll(void)
  70. {
  71.     char buf[MAXSTR];
  72.     APIRET rc;
  73.  
  74.     if (gsdll.hmodule == (HMODULE) NULL)
  75.     return TRUE;
  76.     rc = DosFreeModule(gsdll.hmodule);
  77.     if (rc) {
  78.     sprintf(buf, "DosFreeModule returns %d\n", rc);
  79.     gs_addmess(buf);
  80.     sprintf(buf, "Unloaded GSDLL\n\n");
  81.     gs_addmess(buf);
  82.     }
  83.     return !rc;
  84. }
  85.  
  86. void
  87. gs_load_dll_cleanup(void)
  88. {
  89.     char buf[MAXSTR];
  90.  
  91.     gs_free_dll();
  92.     fprintf(stdout, "Can't load Ghostscript DLL %s", szDllName);
  93. }
  94.  
  95. /* load GS DLL if not already loaded */
  96. /* return TRUE if OK */
  97. BOOL
  98. gs_load_dll(void)
  99. {
  100.     char buf[MAXSTR + 40];
  101.     APIRET rc;
  102.     char *p;
  103.     int i;
  104.     long revision;
  105.     const char *dllname;
  106.     PTIB pptib;
  107.     PPIB pppib;
  108.     char szExePath[MAXSTR];
  109.     char fullname[1024];
  110.     const char *shortname;
  111.  
  112.     if ((rc = DosGetInfoBlocks(&pptib, &pppib)) != 0) {
  113.     fprintf(stdout, "Couldn't get pid, rc = \n", rc);
  114.     return FALSE;
  115.     }
  116.     /* get path to EXE */
  117.     if ((rc = DosQueryModuleName(pppib->pib_hmte, sizeof(szExePath), szExePath)) != 0) {
  118.     fprintf(stdout, "Couldn't get module name, rc = %d\n", rc);
  119.     return FALSE;
  120.     }
  121.     if ((p = strrchr(szExePath, '\\')) != (char *)NULL) {
  122.     p++;
  123.     *p = '\0';
  124.     }
  125.     dllname = szDllName;
  126.     if (debug) {
  127.     sprintf(buf, "Trying to load %s\n", dllname);
  128.     gs_addmess(buf);
  129.     }
  130.     memset(buf, 0, sizeof(buf));
  131.     rc = DosLoadModule(buf, sizeof(buf), dllname, &gsdll.hmodule);
  132.     if (rc) {
  133.     /* failed */
  134.     /* try again, with path of EXE */
  135.     if ((shortname = strrchr((char *)szDllName, '\\')) == (const char *)NULL)
  136.         shortname = szDllName;
  137.     strcpy(fullname, szExePath);
  138.     if ((p = strrchr(fullname, '\\')) != (char *)NULL)
  139.         p++;
  140.     else
  141.         p = fullname;
  142.     *p = '\0';
  143.     strcat(fullname, shortname);
  144.     dllname = fullname;
  145.     if (debug) {
  146.         sprintf(buf, "Trying to load %s\n", dllname);
  147.         gs_addmess(buf);
  148.     }
  149.     rc = DosLoadModule(buf, sizeof(buf), dllname, &gsdll.hmodule);
  150.     if (rc) {
  151.         /* failed again */
  152.         /* try once more, this time on system search path */
  153.         dllname = shortname;
  154.         if (debug) {
  155.         sprintf(buf, "Trying to load %s\n", dllname);
  156.         gs_addmess(buf);
  157.         }
  158.         rc = DosLoadModule(buf, sizeof(buf), dllname, &gsdll.hmodule);
  159.     }
  160.     }
  161.     if (rc == 0) {
  162.     if (debug)
  163.         gs_addmess("Loaded Ghostscript DLL\n");
  164.     if ((rc = DosQueryProcAddr(gsdll.hmodule, 0, "GSDLL_REVISION", (PFN *) (&gsdll.revision))) != 0) {
  165.         sprintf(buf, "Can't find GSDLL_REVISION, rc = %d\n", rc);
  166.         gs_addmess(buf);
  167.         gs_load_dll_cleanup();
  168.         return FALSE;
  169.     }
  170.     /* check DLL version */
  171.     gsdll.revision(NULL, NULL, &revision, NULL);
  172.     if (revision != GS_REVISION) {
  173.         sprintf(buf, "Wrong version of DLL found.\n  Found version %ld\n  Need version  %ld\n", revision, (long)GS_REVISION);
  174.         gs_addmess(buf);
  175.         gs_load_dll_cleanup();
  176.         return FALSE;
  177.     }
  178.     if ((rc = DosQueryProcAddr(gsdll.hmodule, 0, "GSDLL_INIT", (PFN *) (&gsdll.init))) != 0) {
  179.         sprintf(buf, "Can't find GSDLL_INIT, rc = %d\n", rc);
  180.         gs_addmess(buf);
  181.         gs_load_dll_cleanup();
  182.         return FALSE;
  183.     }
  184.     if ((rc = DosQueryProcAddr(gsdll.hmodule, 0, "GSDLL_EXECUTE_BEGIN", (PFN *) (&gsdll.execute_begin))) != 0) {
  185.         sprintf(buf, "Can't find GSDLL_EXECUTE_BEGIN, rc = %d\n", rc);
  186.         gs_addmess(buf);
  187.         gs_load_dll_cleanup();
  188.         return FALSE;
  189.     }
  190.     if ((rc = DosQueryProcAddr(gsdll.hmodule, 0, "GSDLL_EXECUTE_CONT", (PFN *) (&gsdll.execute_cont))) != 0) {
  191.         sprintf(buf, "Can't find GSDLL_EXECUTE_CONT, rc = %d\n", rc);
  192.         gs_addmess(buf);
  193.         gs_load_dll_cleanup();
  194.         return FALSE;
  195.     }
  196.     if ((rc = DosQueryProcAddr(gsdll.hmodule, 0, "GSDLL_EXECUTE_END", (PFN *) (&gsdll.execute_end))) != 0) {
  197.         sprintf(buf, "Can't find GSDLL_EXECUTE_END, rc = %d\n", rc);
  198.         gs_addmess(buf);
  199.         gs_load_dll_cleanup();
  200.         return FALSE;
  201.     }
  202.     if ((rc = DosQueryProcAddr(gsdll.hmodule, 0, "GSDLL_EXIT", (PFN *) (&gsdll.exit))) != 0) {
  203.         sprintf(buf, "Can't find GSDLL_EXIT, rc = %d\n", rc);
  204.         gs_addmess(buf);
  205.         gs_load_dll_cleanup();
  206.         return FALSE;
  207.     }
  208.     if ((rc = DosQueryProcAddr(gsdll.hmodule, 0, "GSDLL_GET_BITMAP", (PFN *) (&gsdll.get_bitmap))) != 0) {
  209.         sprintf(buf, "Can't find GSDLL_GET_BITMAP, rc = %d\n", rc);
  210.         gs_addmess(buf);
  211.         gs_load_dll_cleanup();
  212.         return FALSE;
  213.     }
  214.     if ((rc = DosQueryProcAddr(gsdll.hmodule, 0, "GSDLL_LOCK_DEVICE", (PFN *) (&gsdll.lock_device))) != 0) {
  215.         sprintf(buf, "Can't find GSDLL_LOCK_DEVICE, rc = %d\n", rc);
  216.         gs_addmess(buf);
  217.         gs_load_dll_cleanup();
  218.         return FALSE;
  219.     }
  220.     } else {
  221.     sprintf(buf, "Can't load Ghostscript DLL %s \nDosLoadModule rc = %d\n", szDllName, rc);
  222.     gs_addmess(buf);
  223.     gs_load_dll_cleanup();
  224.     return FALSE;
  225.     }
  226.     return TRUE;
  227. }
  228.  
  229.  
  230. int
  231. read_stdin(char FAR * str, int len)
  232. {
  233.     int ch;
  234.     int count = 0;
  235.  
  236.     while (count < len) {
  237.     ch = fgetc(stdin);
  238.     if (ch == EOF)
  239.         return count;
  240.     *str++ = ch;
  241.     count++;
  242.     if (ch == '\n')
  243.         return count;
  244.     }
  245.     return count;
  246. }
  247.  
  248. int
  249. gsdll_callback(int message, char *str, unsigned long count)
  250. {
  251.     char *p;
  252.  
  253.     switch (message) {
  254.     case GSDLL_STDIN:
  255.         return read_stdin(str, count);
  256.     case GSDLL_STDOUT:
  257.         if (str != (char *)NULL)
  258.         fwrite(str, 1, count, stdout);
  259.         fflush(stdout);
  260.         return count;
  261.     case GSDLL_DEVICE:
  262.         if (count)
  263.         fprintf(stdout, "os2dll device not supported in this version of Ghostscript\n");
  264.         fprintf(stdout, "Callback: DEVICE %p %s\n", str,
  265.             count ? "open" : "close");
  266.         break;
  267.     case GSDLL_SYNC:
  268.         fprintf(stdout, "Callback: SYNC %p\n", str);
  269.         break;
  270.     case GSDLL_PAGE:
  271.         fprintf(stdout, "Callback: PAGE %p\n", str);
  272.         break;
  273.     case GSDLL_SIZE:
  274.         fprintf(stdout, "Callback: SIZE %p width=%d height=%d\n", str,
  275.             (int)(count & 0xffff), (int)((count >> 16) & 0xffff));
  276.         break;
  277.     case GSDLL_POLL:
  278.         return 0;        /* no error */
  279.     default:
  280.         fprintf(stdout, "Callback: Unknown message=%d\n", message);
  281.         break;
  282.     }
  283.     return 0;
  284. }
  285.  
  286. int
  287. main(int argc, char *argv[])
  288. {
  289.     int code;
  290.  
  291.     if (!gs_load_dll()) {
  292.     fprintf(stderr, "Can't load %s\n", szDllName);
  293.     return -1;
  294.     }
  295.     code = gsdll.init(gsdll_callback, (HWND) NULL, argc, argv);
  296.     if (!code)
  297.     code = gsdll.execute_begin();
  298.     if (!code) {
  299.     code = gsdll.execute_cont(start_string, strlen(start_string));
  300.     if (!code) {
  301.         gsdll.execute_end();
  302.         gsdll.exit();
  303.     } else
  304.         code = gsdll.exit();
  305.     }
  306.     gs_free_dll();
  307.     if (code == GSDLL_INIT_QUIT)
  308.     return 0;
  309.     return code;
  310. }
  311.